import {Injectable} from '@angular/core';
import {Http, Headers, Response, RequestOptions} from '@angular/http'
import {Project} from "../entity/Project";
import 'rxjs/add/operator/toPromise';
import {Page} from "../../shared/entity/Page";
import {Report} from "../entity/Report";
import {CommonIp} from "../../user/entity/Ip";


@Injectable()
export class ProjectPutService extends CommonIp{
  private projectPrefix = this.userIp+"/p/project/";
  private projectAddShowPicURL_PREFIX =this.userIp+"/p/project/updateProjectShowPics/";
  constructor(public http:Http){
      super();
  }
  public pointProject(id:string,userId:string){
    let URL = this.projectPrefix+"pointProject/"+id+"/user/"+userId;


    return this.http.put(URL,'',null)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);

  }
  public extractData(res:Response){
    if(res.status<200||res.status>=300){
      throw new Error('Bad response status:'+res.status);
    }
    let body = res.json();
    return body.list||{};
  }
  public handleError(error:any):Promise<any>{
    console.error("An error occurred",error);
    return Promise.reject(error.message||error);
  }

  followProject(id: string, userId: string) {
    let URL = this.projectPrefix+"followProject/"+id+"/user/"+userId;


    return this.http.put(URL,'',null)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }
  public updateShowPics(id: string, showImages: string) {
    let URL = this.projectAddShowPicURL_PREFIX+id+"?picUrls="+showImages;
    return this.http.put(URL,'',null)
      .toPromise()
      .then(this.extractData)
      .catch(this.handleError);
  }


}
